package cn.wikiflyer.jft.utils;

/*
 * Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * @author SINA
 * @since 2013-10-07
 */
public class AccessTokenKeeper {
	public static final String SINAPREFERENCES_NAME = "wiki_sina_sdk_android";
	public static final String QQPREFERENCES_NAME = "wiki_qq_sdk_android";
	public static final String QZONEPREFERENCES_NAME = "wiki_qq_sdk_android";
	public static final String WEIXINPREFERENCES_NAME = "wiki_weixin_sdk_android";

	public static final String KEY_UID = "uid";
	public static final String KEY_ACCESS_TOKEN = "access_token";
//	private static final String KEY_EXPIRES_IN = "expires_in";

	public static void writeAccessTokenUM(String preferences, Context context, Map<String, Object> token) {
		if (null == context || null == token) {
			return;
		}

		SharedPreferences pref = context.getSharedPreferences(preferences,
				Context.MODE_APPEND);
		Editor editor = pref.edit();
//		editor.putLong(KEY_UID, (Long) token.get(KEY_UID));
		editor.putString(KEY_ACCESS_TOKEN, (String) token.get(KEY_ACCESS_TOKEN));
		
		editor.commit();
	}
	public static Map<String, Object> readAccessTokenUM(String preferences, Context context) {
		if (null == context) {
			return null;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		
		SharedPreferences pref = context.getSharedPreferences(preferences,
				Context.MODE_APPEND);
//		map.put(KEY_UID, pref.getLong(KEY_UID, 0));
		map.put(KEY_ACCESS_TOKEN, pref.getString(KEY_ACCESS_TOKEN, ""));
		return map;
	}

	public static void clear(String preferences, Context context) {
		if (null == context) {
			return;
		}

		SharedPreferences pref = context.getSharedPreferences(preferences,
				Context.MODE_APPEND);
		Editor editor = pref.edit();
		editor.clear();
		editor.commit();
	}
}
